﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Notepad_Clone
{
    partial class NotepadClone : Window
    {
        void AddEditMenu(Menu menu)
        {
            MenuItem itemEdit = new MenuItem();
            itemEdit.Header = "_Edit";
            menu.Items.Add(itemEdit);

            MenuItem itemUndo = new MenuItem();
            itemUndo.Header = "_Undo";
            itemUndo.Command = ApplicationCommands.Undo;
            itemEdit.Items.Add(itemUndo);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, this.UndoOnExecute, this.UndoCanExecute));

            MenuItem itemRedo = new MenuItem();
            itemRedo.Header = "_Redo";
            itemRedo.Command = ApplicationCommands.Redo;
            itemEdit.Items.Add(itemRedo);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, this.RedoOnExecute, this.RedoCanExecute));

            itemEdit.Items.Add(new Separator());

            MenuItem itemCut = new MenuItem();
            itemCut.Header = "Cu_t";
            itemCut.Command = ApplicationCommands.Cut;
            itemEdit.Items.Add(itemCut);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, this.CutOnExecute, this.CutCanExecute));

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "_Copy";
            itemCopy.Command = ApplicationCommands.Copy;
            itemEdit.Items.Add(itemCopy);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, this.CopyOnExecute, this.CutCanExecute));

            MenuItem itemPaste = new MenuItem();
            itemPaste.Header = "_Paste";
            itemPaste.Command = ApplicationCommands.Paste;
            itemEdit.Items.Add(itemPaste);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, this.PasteOnExecute, this.PasteCanExecute));

            MenuItem itemDel = new MenuItem();
            itemDel.Header = "De_lete";
            itemDel.Command = ApplicationCommands.Delete;
            itemEdit.Items.Add(itemDel);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, this.DeleteOnExecute, this.CutCanExecute));

            itemEdit.Items.Add(new Separator());

            this.AddFindMenuItems(itemEdit);

            itemEdit.Items.Add(new Separator());

            MenuItem itemAll = new MenuItem();
            itemAll.Header = "Select _All";
            itemAll.Command = ApplicationCommands.SelectAll;
            itemEdit.Items.Add(itemAll);
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, this.SelectAllOnExecute));

            InputGestureCollection coll = new InputGestureCollection();
            coll.Add(new KeyGesture(Key.F5));
            RoutedUICommand commTimeDate = new RoutedUICommand("Time/_Date", "TimeDate", this.GetType(), coll);
            MenuItem itemDate = new MenuItem();
            itemDate.Command = commTimeDate;
            itemEdit.Items.Add(itemDate);
            this.CommandBindings.Add(new CommandBinding(commTimeDate, this.TimeDateOnExecute));
        }

        private void TimeDateOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.SelectedText = DateTime.Now.ToString();
        }

        private void SelectAllOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.SelectAll();
        }

        private void DeleteOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.SelectedText = string.Empty;
        }

        private void PasteCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Clipboard.ContainsText();
        }

        private void PasteOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.Paste();
        }

        private void CopyOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.Copy();
        }

        private void CutCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.txtbox.SelectedText.Length > 0;
        }

        private void CutOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.Cut();
        }

        private void RedoCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.txtbox.CanRedo;
        }

        private void RedoOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.Redo();
        }

        private void UndoCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.txtbox.CanRedo;
        }

        private void UndoOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.txtbox.Undo();
        }
    }
}
